package luongvd.weather.control;

import java.util.ArrayList;

import luongvd.weather.R;
import luongvd.weather.common.Const;
import luongvd.weather.common.Utils;
import luongvd.weather.common.service.utils.NetworkUtil;
import luongvd.weather.model.adapter.AdapterAutocompleteNameCity;
import luongvd.weather.model.adapter.AdapterCity;
import luongvd.weather.model.object.ObjectCity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;

public class SearchCityActivity extends Activity implements OnClickListener{
	LinearLayout ln_back;
	AutoCompleteTextView name_city;
	ImageButton ib_search, ib_delete;
	Button current_location;
	ListView list_city;
	Location myLocation;
	Address myAddress;
	String str_current_address;
	AdapterCity adapter;
	AdapterAutocompleteNameCity adapter_autocomplete;
	ArrayList<ObjectCity> arrayCity;
	ObjectCity objectCity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search_city);
        unitDB();
        unitWidget();
        addAdapterToViews();
	}
	

	private void addAdapterToViews() {
		if(NetworkUtil.checkNetWork(this))
		{
			adapter_autocomplete = new AdapterAutocompleteNameCity(this); 
			name_city.setAdapter(adapter_autocomplete);
			name_city.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

						Address addressCity = adapter_autocomplete.getItem(position);
						objectCity = new ObjectCity();
						objectCity.setLat(String.valueOf(addressCity.getLatitude()));
						objectCity.setLon(String.valueOf(addressCity.getLongitude()));
						objectCity.setName(addressCity.getAdminArea());
//						Log.d("Luong", "=============== lat: "+addressCity.getLatitude());
//						Log.d("Luong", "=============== lon: "+addressCity.getLongitude());
//						Log.d("Luong", "=============== address: "+addressCity.getAdminArea());
					}
				});
		}
	}


	private void unitDB() {
		arrayCity = new ArrayList<ObjectCity>();
		
	}

	private void unitWidget() {
		ln_back = (LinearLayout)findViewById(R.id.ln_back);
		ln_back.setOnClickListener(this);
		ib_delete = (ImageButton)findViewById(R.id.ib_delete);
		ib_delete.setOnClickListener(this);
		name_city = (AutoCompleteTextView)findViewById(R.id.name_city);
		ib_search = (ImageButton)findViewById(R.id.ib_search);
		ib_search.setOnClickListener(this);
		current_location = (Button)findViewById(R.id.current_location);
		current_location.setOnClickListener(this);
		list_city = (ListView)findViewById(R.id.list_city);
		adapter = new AdapterCity(this, arrayCity);
		list_city.setAdapter(adapter);
	}

	// lay vi tri hien tai
	private void getCurrentLocation(Context context) {
		//
		if(NetworkUtil.checkNetWork(context))
		{
			myLocation = Utils.getMyLocation(context);
			Log.d("Luong", "===================== lat:"+myLocation.getLatitude());
			Log.d("Luong", "===================== lon:"+myLocation.getLongitude());
			str_current_address = Utils.getAddressFromLocation(myLocation, this);
		}else{
			Toast.makeText(getBaseContext(), getString(R.string.internet_not_connect_), Toast.LENGTH_SHORT).show();
		}
	}	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ln_back:
			finish();
			break;
		case R.id.ib_delete:
			objectCity = null;
			name_city.setText("");
			break;
		case R.id.ib_search:
			if(objectCity!=null)
			{
			//convert object to json
			Gson gson = new Gson();
			String jsonObjectCity = gson.toJson(objectCity);
			
			Intent returnIntent = new Intent();
			returnIntent.putExtra(Const.KEY_JSON_CITY,jsonObjectCity);
			setResult(RESULT_OK,returnIntent);     
			finish();
			}else{
				Toast.makeText(getBaseContext(), getString(R.string.please_choose_location_truth), Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.current_location:
			getCurrentLocation(this);
			if(str_current_address != null)
			{
//				arrayCity.clear();
				objectCity = new ObjectCity();
				objectCity.setDescription(getString(R.string.current_location));
				objectCity.setLat(String.valueOf(myLocation.getLatitude()));
				objectCity.setLat(String.valueOf(myLocation.getLongitude()));
				objectCity.setName(str_current_address);
				name_city.setText(str_current_address);
//			Log.d("Luong", "============ my lat: "+myLocation.getLatitude());
//			Log.d("Luong", "============ my lon: "+myLocation.getLongitude());
//			Log.d("Luong", "============ my address: "+str_current_address);
//				arrayCity.add(objectCity);
//				adapter.notifyDataSetChanged();
			}else{
				Toast.makeText(getBaseContext(), getString(R.string.not_get_current_location_please_try_again_), Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
		
	}

}
